package com.task.submitter.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskHandle;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.repackaged.com.google.api.client.http.HttpResponse;
import com.google.appengine.repackaged.com.google.api.client.util.Lists;
import com.google.gson.Gson;


@Controller
public class HelloWorldController extends BaseController{

	private static final String HELLO = "hello";
	private static final String VOLTAGE_FLUX = "/submit/voltagetask";
	private static final String MEMBRANE_FLUX = "/submit/membranetask";
	
	@RequestMapping("/voltageflux")
	public ModelAndView voltageflux(HttpServletRequest request, HttpServletResponse response) throws IOException{
	    String email = getEmail(request, response);
		TaskOptions options = TaskOptions.Builder.withUrl(VOLTAGE_FLUX)
				.param("user", email)
				.param("steps", "10")
				.param("dim", "10")
				.param("temperature", "36.6");
		TaskHandle handle = QueueFactory.getQueue("tasks").add(options);
	   
	    ModelAndView mav = new ModelAndView(HELLO);
	    mav.getModel().put("email", email);
	    mav.getModel().put("tasks", getStoredFluxTasks(email));
	    mav.getModel().put("myName", "Voltage flux");
		return mav;
	}
	
	
	private List<String> getStoredFluxTasks(String user){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key userCtxKey = KeyFactory.createKey("UserContext", user);
	    Query query = new Query("Flux", userCtxKey);
	    List<Entity> results = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());

	    List<String> output = Lists.newArrayList();
	    for(Entity result : results){
	    	Object property = result.getProperty("temperature");
	    	output.add(property == null ? "NULL" : String.valueOf(property));
	    	Gson gson = new Gson();
	    	List<List<Double>> data = gson.fromJson(((Text)result.getProperty("data")).getValue(), List.class);
	    }
	    
	    return output;
	}
	
	
	@RequestMapping("/membraneshape")
	public ModelAndView membraneshape(HttpServletRequest request, HttpServletResponse response) throws IOException{
	    String email = getEmail(request, response);
		TaskOptions options = TaskOptions.Builder.withUrl(MEMBRANE_FLUX)
				.param("user", email)
				.param("steps", "10")
				.param("dim", "10")
				.param("temperature", "100");
		TaskHandle handle = QueueFactory.getQueue("tasks").add(options);
	   
	    ModelAndView mav = new ModelAndView(HELLO);
	    mav.getModel().put("email", email);
	    mav.getModel().put("tasks", getStoredMembraneTasks(email));
	    mav.getModel().put("myName", "Membrane shape");
		return mav;
	}
	
	
	private List<String> getStoredMembraneTasks(String user){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key userCtxKey = KeyFactory.createKey("UserContext", user);
	    Query query = new Query("Membrane", userCtxKey);
	    List<Entity> results = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());

	    List<String> output = Lists.newArrayList();
	    for(Entity result : results){
	    	Object property = result.getProperty("temperature");
	    	output.add(property == null ? "NULL" : String.valueOf(property));
	    	Gson gson = new Gson();
	    	List<List<Double>> data = gson.fromJson(((Text)result.getProperty("data")).getValue(), List.class);
	    }
	    
	    return output;
	}
	
}
